import { View, Text, Button, Input, Textarea } from "@tarojs/components"
import Taro, { getCurrentPages, getStorageSync, useDidShow } from '@tarojs/taro'
import Comments from "../../components/comments/comments"
import { AtAccordion, AtButton, AtForm, AtList, AtTextarea } from "taro-ui"
// import enrollComments from "../../components/enroll_comments/enroll_comments"
import { useEffect, useRef, useState } from "react"
import NavBar from "../topbar/topbar"
import { values } from "lodash"
import "../../pages/test/test.scss"

const Publication_onself = () => {

    //用于标题栏是否打开,默认关闭
    const [isopen1, setOpen1] = useState(true)
    const [isopen2, setOpen2] = useState(false)
    const [isopen3, setOpen3] = useState(false)
    // 各个数据储存
    const [morning, setMorning] = useState(undefined)
    const [aft, setAft] = useState(undefined)
    const [night, setNight] = useState(undefined)
    //用来零时保存数据
    // const [data, setData] = useState(undefined)
    // const [data1, setData1] = useState(undefined)
    // const [data2, setData2] = useState(undefined)
    // const [data3, setData3] = useState(undefined)
    // const [list, setList] = useState([])
    //用来保存用户输入的数据
    const list = useRef({})
    const [lists, setLists] = useState([])
    //更新零时保存数据
    // const clearDt = () => {
    //     setData(undefined)
    //     setData1(undefined)
    //     setData2(undefined)
    //     setData3(undefined)
    // }
    // const monData = useRef();


    //当每个导航栏关闭时就会保存当前的数据
    let newList = [{}]
    const Updta = (num) => {
        // let count=0
        // 最后的提交按钮的操作，把之前三个时间段的数据进行更新
        // useEffect(() => {
        switch (num) {
            case 0:
                setOpen1(!isopen1)//改变状态
                // let newList = [data, data1, data2]
                // let newList=data
                // console.log("newlist:", newList)
                // setMorning(newList)
                break
            case 1:
                // clearDt()
                setOpen2(!isopen2)//改变状态
                // let newList1 = [data, data1, data2, data3]
                // let newList1=data1
                // setAft(newList1)
                // clearDt()
                // setAft(data)
                // data.splice(0)
                break
            case 2:
                // clearDt()
                setOpen3(!isopen3)//改变状态
            // // let newList2 = [data, data1, data2]
            // let newList2=data2
            // setNight(newList2)
            // // clearDt()
            // break
        }
        // }, []);
        newList = [{
            morning: morning,
            aft: aft,
            night: night
        }]
        //写入数组
        // list.current = newList

        setLists(newList)

    }
    // console.log("list:", list.current[0])
    console.log("newlists:", lists)

    // useDidShow(() => {
    //     setLists(newList)
    // })
    //调用云函数
    const handleUpdata = () => {
        console.log("你点击的submit")

        Taro.cloud.callFunction({
            name: "publication",
            data: {
                $url: "Update",
                data: lists[0],
                reception_person: "小男孩",
            }
        }).then(() => {
            Taro.showModal({
                title: '温馨提示',
                content: '您已成功提交',
                showCancel: false
            });
            return;
        })
    }
    // 返回上一级
    const toBack = () => {
        Taro.navigateBack().then();
    }
    // 返回主页
    const toHome = () => {
        Taro.redirectTo({ url: "/pages/my/index" }).then();
    }
    return (
        <View style="background-color: antiquewhite;">
            <NavBar
                // onClickLeftIcon={handleClick1}
                color="#000"
                title="发表营养规划"
                back={true}
                nback={toBack}
                home={true}
                onHome={toHome}
            />
            {/* <AtForm
                onSubmit={Updta}
            // onReset={},(count=0)=>{count+1})
            >    */}
            {/* 标题一 */}
            <View style="z-index: 2;">
                <AtAccordion
                    open={isopen1}
                    onClick={() => { Updta(0) }}
                    title='妈妈的上午规划'
                >
                    <AtList hasBorder={false}>
                        <AtForm>
                            <AtTextarea
                                value={morning}
                                onChange={(value) => { setMorning(value) }}
                                maxLength={100}
                                placeholder='清晨的规划...'
                            />
                            {/* <AtTextarea
                                value={(morning !== undefined ? morning[1] : data1)}
                                onChange={(value) => { setData1(value) }}
                                maxLength={100}
                                placeholder='9:00-10:30...'
                            />
                            <AtTextarea
                                value={(morning !== undefined ? morning[2] : data2)}
                                onChange={(value) => { setData2(value) }}
                                maxLength={100}
                                placeholder='10:30-12:00...'
                            /> */}
                            {/* 区域重置 */}
                            {/* <AtButton formType='reset'>重置</AtButton> */}
                        </AtForm>
                    </AtList>
                </AtAccordion>

                <AtAccordion
                    open={isopen2}
                    onClick={() => { Updta(1) }}
                    title='妈妈的下午规划'
                >
                    {/* <AtList hasBorder={false}> */}

                    <AtTextarea
                        value={aft}
                        onChange={value => { setAft(value) }}
                        maxLength={100}
                        placeholder='中午的规划...'
                    />
                    {/* <AtTextarea
                            value={data1 !== undefined ? undefined : (aft !== undefined ? aft[1] : data1)}
                            onChange={value => { setData1(value) }}
                            maxLength={100}
                            placeholder='13:00-14:30...'
                        />
                        <AtTextarea
                            value={data2 !== undefined ? undefined : (aft !== undefined ? aft[2] : data2)}
                            onChange={value => { setData2(value) }}
                            maxLength={100}
                            placeholder='14:30-16:00...'
                        />
                        <AtTextarea
                            value={data3 !== undefined ? undefined : (aft !== undefined ? aft[3] : data3)}
                            onChange={value => { setData3(value) }}
                            maxLength={100}
                            placeholder='16:00-17:30...'
                        /> */}
                    {/* </AtList> */}
                </AtAccordion>

                <AtAccordion
                    open={isopen3}
                    onClick={() => { Updta(2) }}
                    title='妈妈的晚上规划'
                >
                    {/* <AtList hasBorder={false}> */}
                    <AtTextarea
                        value={night}
                        onChange={value => { setNight(value) }}
                        maxLength={100}
                        placeholder='晚上规划...'
                    />
                    {/* <AtTextarea
                            value={data1 !== undefined ? undefined:(night !== undefined ? night[1] : data1)}
                            onChange={value => { setAft(value) }}
                            maxLength={100}
                            placeholder='妈妈下午午的规划...'
                        />
                        <AtTextarea
                            value={data2 !== undefined ? undefined: (night !== undefined ? night[2] : data2)}
                            onChange={value => { setNight(value) }}
                            maxLength={100}
                            placeholder='妈妈晚上的规划...'
                        /> */}
                    {/* </AtList> */}
                </AtAccordion>
                <View className="button" >
                    <AtButton onClick={() => { handleUpdata() }}>提交</AtButton>
                    <AtButton formType='reset'>重置</AtButton>
                </View>
            </View>
        </View>
    )
}
export default Publication_onself
